<template>
  <div>
    <Row style="background:#fff;">
      <Col span="16">
        <Input type="text" v-model="namespace" style="position:fixed;width:50%" />
      </Col>
      <Col span="8">
        <ButtonGroup shape="circle" style="position:fixed;right:20px;">
          <Button :type="enableAttributes ? 'primary' : 'default'" icon="md-bookmarks" @click="enableAttributes = !enableAttributes">{{ enableAttributes ? 'Dapper实体' : '普通对象' }}</Button>
          <Button type="success" icon="md-copy" @click="onCopyScript" style="float:right;margin-right: 2px;">复制代码</Button>
        </ButtonGroup>
      </Col>
    </Row>
    <Row style="margin-top:22px;">
      <pre ref="scripts">
using System;<template v-if="enableAttributes">
using System.ComponentModel.DataAnnotations;
using Mercury.Prime.Core;</template>

namespace {{namespace}}.{{ enableAttributes ? 'Entities' : 'Queries.Responses'}}
{
    /// &lt;summary>
    /// {{$route.query.comment}}实体
    /// &lt;/summary><template v-if="enableAttributes">
    [Table("{{$route.params.tableName}}")]</template>
    public class {{(columns[0].table || $route.params.tableName) | pascal}}
    {<template v-for="item of columns">
        /// &lt;summary>
        /// {{item.column_comment || item.column_name}}
        /// &lt;/summary><template v-for="attr of getAttributes(item)">
        {{attr}}</template>
        public {{item.column_type | codeType(item.is_nullable)}} {{item.column_name | pascal}} { get; set; }
    </template>}
}</pre>
    </Row>
  </div>
</template>
<script>
export default {
  data() {
    return {
      columns: [],
      enableAttributes: false,
      namespace: 'Mercury.AuthCenter'
    };
  },
  watch: {
    $route() {
      this.GetColumns();
    }
  },
  methods: {
    GetColumns() {
      let dbIndex = this.$route.params.dbIndex;
      let schema = this.$route.params.schema;
      let tableName = this.$route.params.tableName;

      if (dbIndex && schema && tableName) {
        this.$nextTick(() => {
          this.$http.get(`/api/${this.$route.query.dbtype}/${dbIndex}/${schema}/${tableName}/columns`).then(res => (this.columns = res.datas));
        });
      }
    },
    getAttributes(item) {
      let rs = [];

      if (!this.enableAttributes) {
        return rs;
      }

      let columnAttr = `[Column("${item.column_name}"`;
      let primaryKey = item.column_key == 1 || item.column_key == 'PRI';
      let nullable = item.is_nullable == 1 || item.is_nullable == 'YES';

      if (primaryKey) {
        columnAttr += `, IsPrimaryKey = true`;
      } else {
        columnAttr += `, IsNullable = ${nullable ? 'true' : 'false'}`;
      }

      rs.push(`${columnAttr})]`);

      if (primaryKey || !nullable) {
        rs.push(`[Required(AllowEmptyStrings = false, ErrorMessage = "${item.column_comment}不能为空！")]`);
      }

      let columnType = item.column_type.toLowerCase();

      if (columnType.indexOf('char') != -1 && columnType.indexOf('max') == -1) {
        let counter = columnType.indexOf('n') != -1 ? 2 : 1;

        rs.push(`[StringLength(${item.character_length / counter}, ErrorMessage = "${item.column_comment}字符长度不能超过{1}个字符！")]`);
      }

      return rs;
    },
    onCopyScript() {
      this.$copyText(this.$refs.scripts.innerText).then(() => {
        this.$Message.success('代码已复制！');
      });
    }
  },
  mounted() {
    this.GetColumns();
  }
};
</script>
